<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 活动管理
 */

namespace Admin\Controller;
use Model\GoodsModel;
use Model\GoodsPicModel;

class GoodsController extends BaseController{
    public $gModel;
    public $gpModel;
    public function __construct(){
        parent::__construct();
        $this->gModel = new GoodsModel();
        $this->gpModel = new GoodsPicModel();
    }
    //商品列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['name'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $resList = $this->gModel->search($where,$page,$this->pernum);
        $totalNum = $this->gModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('goods/index');
    }
    //新增商品
    public function addGoods(){
        if(IS_POST){
            $timeNow = date('Y-m-d H:i:s');
            $gdata = [];
            $gdata['name'] = $_POST['name'];
//            $gdata['need_money'] = $_POST['need_money'];
            $gdata['need_coin'] = $_POST['need_coin'];
            $gdata['addtime'] = $timeNow;
            $gdata['info'] = $_POST['info'] ? $_POST['info'] : '';
            $gid = $this->gModel->addData($gdata);
            foreach ($_POST['mainPic'] as $val){
                $gpArr = [];
                $gpArr['gid'] = $gid;
                $gpArr['pic'] = $val;
                $gpArr['type'] = 1;
                $this->gpModel->addData($gpArr);
            }
            foreach ($_POST['infoPic'] as $val){
                $gpArr = [];
                $gpArr['gid'] = $gid;
                $gpArr['pic'] = $val;
                $gpArr['type'] = 2;
                $this->gpModel->addData($gpArr);
            }
            $this->logModel->log('添加商品','goods',$gid);
            $this->printOut(['code'=>0]);
        }
        //获取分组
        $this->display('goods/addgoods');
    }
    //编辑商品
    public function editGoods(){
        if(IS_POST){
            $gdata = [];
            $id = $_POST['id'];
            $gdata['name'] = $_POST['name'];
//            $gdata['need_money'] = $_POST['need_money'];
            $gdata['need_coin'] = $_POST['need_coin'];
            $gdata['info'] = $_POST['info'] ? $_POST['info'] : '';
            $this->gModel->editData(['id'=>$id],$gdata);
            $this->gpModel->delData(['gid'=>$id]);
            foreach ($_POST['mainPic'] as $val){
                $gpArr = [];
                $gpArr['gid'] = $id;
                $gpArr['pic'] = $val;
                $gpArr['type'] = 1;
                $this->gpModel->addData($gpArr);
            }
            foreach ($_POST['infoPic'] as $val){
                $gpArr = [];
                $gpArr['gid'] = $id;
                $gpArr['pic'] = $val;
                $gpArr['type'] = 2;
                $this->gpModel->addData($gpArr);
            }
            $this->logModel->log('编辑商品','goods',$id);
            $this->printOut(['code'=>0]);
        }

        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->gModel->findData(['id'=>$_GET['id']]);
        $gpList = $this->gpModel->selectData(['gid'=>$_GET['id']]);
        $gpRes = [];
        foreach ($gpList as $val){
            $gpRes[$val['type']][] = $val['pic'];
        }
        $this->assign('gpRes',$gpRes);
        $this->assign('selfInfo',$selfInfo);
        $this->display('goods/editgoods');
    }
    //上下架商品
    public function freezeGoods(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->gModel->editData($where,$data);
        $this->logModel->log('冻结|解冻商品','goods',$_GET['id']);
        redirect($this->domianUrl.'/goods/index?p='.$_GET['page'].'&tip=1');
    }
    //删除商品
    public function delGoods(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->gModel->editData($where,$data);
        $this->logModel->log('删除商品','goods',$_GET['id']);
        redirect($this->domianUrl.'/goods/index?p='.$_GET['page'].'&tip=1');
    }
}